package com.person.leetcode.editor.cn;

/**
 * 66.加一
 * 2022-05-10 15:30:39
 **/
public class Sequence_0066_PlusOne {
    public static void main(String[] args) {
        Solution solution = new Sequence_0066_PlusOne().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int[] plusOne(int[] digits) {
            int pre = 1;
            for (int i = digits.length - 1; i >= 0; i--) {
                int d = digits[i];
                if (d + pre < 10) {
                    digits[i] = d + pre;
                    pre = 0;
                } else {
                    digits[i] = (d + pre) % 10;
                    pre = 1;
                }
            }
            if (1 == pre){
                int[] ints = new int[digits.length+1];
                ints[0] = 1;
                for (int i = 0; i < digits.length; i++) {
                    ints[i+1] = digits[i];
                }
                return ints;
            }else{
                return digits;
            }
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
